package com_.dxy_test_.diy_tomcat_;

import lombok.Data;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * @Description 自定义响应类
 * @Author dangxianyue
 * @Date 2022/9/26 15:31
 */
@Data
public class MyResponse {

    private OutputStream outputStream;

    public MyResponse(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void write(String content) throws IOException {
        StringBuilder responseStr = new StringBuilder();
        //响应行
        responseStr.append("HTTP/1.1 200 OK\n");
        //响应头+换行
        responseStr.append("content-type:text/html;charset=utf-8\n\n");
        //响应体
        responseStr.append("<html>");
        responseStr.append("<title>Abl Tomcat</title>");
        responseStr.append("<h1> " + content + " </h1>");
        responseStr.append("</html>");

        PrintWriter writer = new PrintWriter(this.outputStream);
        writer.print(responseStr);
        writer.flush();
        writer.close();

    }

}
